package com.hspedu.threaduse;

/**
 * @author DL5O
 * @version 1.0
 * 通过实现接口Runnable 来开发线程
 */
public class Thread02 {
    public static void main(String[] args){
        Dog dog = new Dog();
        //dog.start(); 这里不能调用start
        //创建了Thread对象，把 dog对象,实现了Runnable,放入thread
        Thread thread = new Thread(dog);
        thread.start();

        /*Tiger tiger = new Tiger();//实现了runnable接口
        ThreadProxy threadProxy = new ThreadProxy(tiger);
        threadProxy.start();
        System.out.println("哈哈哈哈");*/
        System.out.println("哈哈哈");
    }
}

class Animal{}

class Tiger extends Animal implements Runnable{

    @Override
    public void run() {
        System.out.println("老虎嗷嗷叫...");
    }
}

//模拟了最简的Thread类
class ThreadProxy implements Runnable{//可以当做ThreadProxy ,线程代理

    private Runnable target = null;//属性，类型是Runnable

    @Override
    public void run() {
        if(target != null){
            target.run();//进行动态绑定 运行类型是tiger，即实现了Runnable的类
        }
    }

    public ThreadProxy(Runnable target) {
        this.target = target;
    }

    public void start(){
        start0();//这个方法是真正实现多线程的方法
    }

    private void start0() {
        run();
    }

}

class Dog implements Runnable{

    int count = 0;
    @Override
    public void run() {//普通方法，并没有启动线程
        while(true){
            System.out.println("小狗汪汪叫..hi" + (++count) + Thread.currentThread().getName());
            //休眠一秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(count == 10){
                break;
            }
        }

    }
}